package com.itmei.weather.service;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.itmei.weather.utils.WeatherUtil;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

/**
 * @Author Itmei
 * @Date 2022/9/7 11:09
 * @Version 1.0
 */
@Service
public class WeatherService {

    /**
     * 启动程序时会提示输入天气情况
     * 查询完成后接着提示天气情况
     */
    @PostConstruct
    public void init(){
        boolean flag=true;
        while (flag){
            Scanner sc=new Scanner(System.in);
            System.out.println("请输入想要查询的城市：");
            if (!sc.hasNext()){
                flag=false;
            }
            String input = sc.next();
            try {
                selectCity(input);
            } catch (Exception e) {
                //e.printStackTrace();
                System.out.println(e.getMessage());
            }
        }
    }
    public void selectCity(String city){
        JSONObject realWeather = WeatherUtil.getRealWeather(city);
        System.out.println("》》》》实况天气《《《《");
        System.out.println(realWeather.getStr("cityname")
                +" 天气："+ realWeather.getStr("weather")
                +" 温度:"+ realWeather.getStr("temp")
                +" 湿度:"+ realWeather.getStr("sd")
                +" Pm2.5:"+realWeather.getStr("aqi_pm25")
        );
        JSONObject weather = WeatherUtil.getWeather(city);
        System.out.println("》》》》当天天气《《《《");
        System.out.println(weather.getStr("cityname")
                +" 最低温度："+ weather.getStr("tempn")
                +" 最高温度:"+ weather.getStr("temp")
                +" 风向:"+ weather.getStr("wd")
                +" 风速:"+weather.getStr("ws")
        );

        JSONObject weatherZs = WeatherUtil.getWeatherZs(city);
        System.out.println("小助手提示："+weather.getStr("cityname")+"\n"
                +" 紫外线："+ weatherZs.getStr("uv_hint")+"\n"
                +" 运动指数："+ weatherZs.getStr("yd_hint")+"\n"
                +" 干燥指数："+ weatherZs.getStr("gz_des_s")+"\n"
                +" 约会指数:"+ weatherZs.getStr("yh_hint")+"    理由:"+ weatherZs.getStr("yh_des_s")+"\n"
        );
    }
}
